package hot_100;

public class 回文链表 {
    public static void main(String[] args) {

    }

    public boolean isPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode mid = slow;
        ListNode befor = null;
        ListNode now = slow;
        while (mid != null) {
            now = mid;
            mid = mid.next;
            now.next = befor;
            befor = now;
        }
        while (head != null) {
            if (head.val != now.val)
                return false;
            head = head.next;
            now = now.next;
        }
        return true;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
